#include <iostream>
#include <cstring>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <functional>
using namespace std;

// typedef function<void()> func_t;
using func_t = function<void()>;

//传递线程对象
class ThreadData
{
public:
    ThreadData(const string &name, const uint64_t &ctime, func_t f)
        :threadname(name)
        ,createtime(ctime)
        ,func(f)
        {}
public:
    string threadname;
    uint64_t createtime;
    func_t func;
};

void Print()
{
    cout << "我是线程执行的大任务的一部分" << endl;
}

//新线程
void* ThreadRoutine(void* args)
{
    ThreadData* td = static_cast<ThreadData*>(args); //安全强转
    while(true)
    {
        cout << "new thread" << ", thread name :" << td->threadname 
        << ", create time : " << td->createtime << endl;
        td->func();
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    ThreadData* td = new ThreadData("thread-1", (uint64_t)time(nullptr), Print);
    pthread_create(&tid, nullptr, ThreadRoutine, td); 
    while(true)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}
